#include <stdio.h>
#include "sysconfig.h"

void sysconfig(void)
{
    // I2C vers LCD03
	// P0.0 = SDA1, P0.1 = SCL1

    // PIN CONFIG pour I2C1 -> fonction '11' pour bit0 a bit3
	LPC_PINCON->PINSEL0 |= (bit0|bit1|bit2|bit3);

	// MODE -> ni resistance pull-up, ni pull-down  -> fonction '01'
	LPC_PINCON->PINMODE0 |= (bit1|bit3);
	LPC_PINCON->PINMODE0 &= ~(bit0|bit2);

	// MODE OD -> pins en Open Drain -> bit0 & 1 a 1
	LPC_PINCON->PINMODE_OD0 |= (bit0|bit1);

	// on prend PCLKSEL1 = CCLK -> fonction '10'
	// PCLKSEL1 (pour I2C1) =  CCLK -> bit6=0, bit7=1
	LPC_SC->PCLKSEL1 |= bit7;
	LPC_SC->PCLKSEL1 &= ~bit6;

    // CCLK = 100 MH et on veut I2C clock = 100 kHz
	// I2Cfreq = PCLK/(I2C1SCLH+I2SCLL) -> 100.000.000/1.000=100.000 => CLH+CLL=1000
	LPC_I2C1->I2SCLH = 500;
	LPC_I2C1->I2SCLL = 500;  // 500, 500 -> Duty cycle 50% 50%
  //printf("-> I2C1 configured\n");

	// Master Transmit Mode
	LPC_I2C1->I2CONSET |= (bit6); // I2EN
	LPC_I2C1->I2CONCLR |= (bit5|bit4|bit3|bit2); // STA, STO, SI & AA reset
  //printf("-> Master Transmit\n");

    //- Enable Interrupt
  NVIC_EnableIRQ(I2C1_IRQn);
  //printf("-> interruption I2C1 activee\n");

  LPC_I2C1->I2CONSET = 0x0000004C; //- I2EN<6>| SI<3> |AA<2>
  //printf("0x4C\n");

}
